//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package flc.ast.view;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.RectF;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.os.Build.VERSION;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;

import com.blankj.utilcode.util.ConvertUtils;

import easily.tech.guideview.lib.GuideViewBundle;
import easily.tech.guideview.lib.Utils;

@SuppressLint({"ViewConstructor"})
final class GuideView extends RelativeLayout {
    private boolean hasAddHintView = false;
    public boolean isShowing = false;
    private int[] targetViewLocation = new int[2];
    private int targetViewWidth;
    private int targetViewHeight;
    private int screenWidth;
    private int screenHeight;
    private Paint backgroundPaint;
    private Paint transparentPaint;
    private GuideViewBundle bundle;
    private FrameLayout decorView;

    GuideView(Context context, GuideViewBundle bundle) {
        super(context);
        this.bundle = bundle;
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        this.screenWidth = displayMetrics.widthPixels;
        this.screenHeight = displayMetrics.heightPixels;
        this.backgroundPaint = new Paint();
        this.transparentPaint = new Paint();
        this.backgroundPaint.setColor(bundle.getMaskColor());
        this.decorView = (FrameLayout) ((Activity) this.getContext()).getWindow().getDecorView();
    }

    protected void onDraw(Canvas canvas) {
        if (this.bundle != null) {
            this.drawBackGround(canvas);
        }
    }

    private void drawBackGround(Canvas canvas) {
        Bitmap bitmap;
        if (canvas.getWidth() != 0 && canvas.getHeight() != 0) {
            bitmap = Bitmap.createBitmap(canvas.getWidth(), canvas.getHeight(), Config.ARGB_8888);
        } else {
            bitmap = Bitmap.createBitmap(this.screenWidth, this.screenHeight, Config.ARGB_8888);
        }

        Canvas backGround = new Canvas(bitmap);
//        backGround.drawRect(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight() - ConvertUtils.dp2px(104), this.backgroundPaint);
        backGround.drawRect(0.0F, 0.0F, (float) canvas.getWidth(), (float) canvas.getHeight(), this.backgroundPaint);
        PorterDuffXfermode mDrawMode = new PorterDuffXfermode(Mode.DST_OUT);
        this.transparentPaint.setXfermode(mDrawMode);
        this.transparentPaint.setAntiAlias(true);
        if (this.bundle.isHasTransparentLayer()) {
            int extraHeight = VERSION.SDK_INT < 19 ? Utils.getStatusBarHeight(this.getContext()) : 0;
            float left = (float) (this.targetViewLocation[0] - this.bundle.getTransparentSpaceLeft());
            float top = (float) (this.targetViewLocation[1] - this.bundle.getTransparentSpaceTop() - extraHeight);
            float right = (float) (this.targetViewLocation[0] + this.targetViewWidth + this.bundle.getTransparentSpaceRight());
            float bottom = (float) (this.targetViewLocation[1] + this.targetViewHeight + this.bundle.getTransparentSpaceBottom() - extraHeight);
            RectF rectF = new RectF(left, top, right, bottom);
            switch (this.bundle.getOutlineType()) {
                case 0:
                    backGround.drawOval(rectF, this.transparentPaint);
                    break;
                case 1:
                    backGround.drawRoundRect(rectF, 20, 20, this.transparentPaint);
                    break;
                default:
                    backGround.drawOval(rectF, this.transparentPaint);
            }
        }

        canvas.drawBitmap(bitmap, 0.0F, 0.0F, this.backgroundPaint);
    }

    private void addHintView() {
        if (!this.hasAddHintView && this.bundle.getHintView() != null) {
            RelativeLayout.LayoutParams params = this.bundle.getHintViewParams() == null ? new RelativeLayout.LayoutParams(-2, -2) : this.bundle.getHintViewParams();
            int bottom = 0;
            int right = 0;
            int top = 0;
            int left = 0;
            int gravity = 8388659;
            int viewHeight = this.decorView.getHeight();
            int extraHeight = VERSION.SDK_INT < 19 ? Utils.getStatusBarHeight(this.getContext()) : 0;
            switch (this.bundle.getHintViewDirection()) {
                case 1:
                    gravity = 8388613;
                    top = this.targetViewLocation[1] + this.bundle.getHintViewMarginTop();
                    right = this.screenWidth - this.targetViewLocation[0] + this.bundle.getHintViewMarginRight() + this.bundle.getTransparentSpaceLeft();
                    break;
                case 2:
                    gravity = 8388611;
                    top = this.targetViewLocation[1] + this.bundle.getHintViewMarginTop();
                    left = this.targetViewLocation[0] + this.targetViewWidth + this.bundle.getHintViewMarginLeft() + this.bundle.getTransparentSpaceRight();
                    break;
                case 3:
                    gravity = 80;
                    bottom = viewHeight - this.targetViewLocation[1] + this.bundle.getHintViewMarginBottom() + this.bundle.getTransparentSpaceTop();
                    left = this.targetViewLocation[0] + this.bundle.getHintViewMarginLeft();
                    break;
                case 4:
                    gravity = 48;
                    top = this.targetViewLocation[1] + this.targetViewHeight + this.bundle.getHintViewMarginTop() + this.bundle.getTransparentSpaceBottom() - extraHeight;
                    left = this.targetViewLocation[0] + this.bundle.getHintViewMarginLeft();
            }

            this.setGravity(gravity);
            params.setMargins(left, top, right, bottom);
            if (this.bundle.getHintView().getParent() != null) {
                this.bundle.getHintView().setLayoutParams(params);
            } else {
                this.addView(this.bundle.getHintView(), params);
            }

            this.hasAddHintView = true;
        }
    }

    private boolean getTargetViewPosition() {
        View targetView = this.bundle.getTargetView();
        if (targetView.getWidth() > 0 && targetView.getHeight() > 0) {
            targetView.getLocationInWindow(this.targetViewLocation);
            this.targetViewWidth = targetView.getWidth();
            this.targetViewHeight = targetView.getHeight();
            if (this.targetViewLocation[0] >= 0 && this.targetViewLocation[1] > 0) {
                return true;
            }
        }

        return false;
    }

    public void show() {
        if (this.bundle.getTargetView() != null) {
            this.bundle.getTargetView().post(new Runnable() {
                public void run() {
                    GuideView.this.showReal();
                }
            });
        }
    }

    private void showReal() {
        boolean hasMeasure = this.getTargetViewPosition();
        if (!this.isShowing && hasMeasure) {
            this.addHintView();
            this.setBackgroundColor(0);
            if (this.getParent() != null && this.getParent() instanceof View) {
                ((View) this.getParent()).setBackgroundColor(0);
            }

            this.isShowing = true;
        }
    }

    public void hide() {
        this.removeAllViews();
        if (this.getParent() != null && this.getParent() instanceof ViewGroup) {
            ((ViewGroup) this.getParent()).removeView(this);
        }

    }
}
